package com.one.shopping.util;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.one.shopping.adapter.ShopGoodsAdapter;
import com.one.shopping.entity.Goods;
import com.one.shopping.ui.fragment.ShopGoodsFragment;

import java.math.BigDecimal;

/**
 * @author 黄金伟
 * @date 创建时间：2016-1-8 上午11:06:58
 * @version 1.0
 */
public class MyClick implements OnClickListener {

	/** 存储的文件名 */
	public static final String DATABASE = "shoppingcart";
	/** 存储后的文件路径：/data/data/<package name>/shares_prefs + 文件名.xml */
	public static final String PATH = "/data/data/code.sharedpreferences/shared_prefs/shoppingcart.xml";

	public EditText etEditText;
	private Context context;
	private int index;
	private Goods good;
	private SharedPreferences mySharedPreferences;
	private Editor editor;
	private ShopGoodsAdapter adapter;
	private int position;

	public MyClick(Context context, Goods good, EditText et, int index, ShopGoodsAdapter adapter, int position) {
		this.context = context;
		this.good = good;
		this.etEditText = et;
		this.index = index;
		this.adapter = adapter;
		this.position = position;
	}

	@Override
	public void onClick(View arg0) {
		if (index == 0) {// 0表示加，1表示减
			if ((Integer.valueOf(etEditText.getText().toString()) + 1)>good.getStock()) {
				Toast.makeText(context, "最大数量不能超过库存量", Toast.LENGTH_SHORT).show();
			}else{
				etEditText.setText("" + (Integer.valueOf(etEditText.getText().toString()) + 1));
				RefreshShoppinglist(context);
				RefreshGoodsSelect();
			}


		} else {
			if ((Integer.valueOf(etEditText.getText().toString())) > 0) {
				etEditText.setText("" + (Integer.valueOf(etEditText.getText().toString()) - 1));
				RefreshShoppinglist(context);
				RefreshGoodsSelect();
			}

			if (adapter != null) {
				if ((Integer.valueOf(etEditText.getText().toString())) == 0) {
					adapter.deleteItem(position);
					//adapter.remove(position);
				}
			}
		}
	}

	/**
	 * 计算总金额
	 */
	public void RefreshGoodsSelect() {
		ShopGoodsFragment.total = 0;
		String id = mySharedPreferences.getString("id", "");
		String price = mySharedPreferences.getString("price", "");
		String quantity = mySharedPreferences.getString("quantity", "");
		if (id != "") {
			String ids[] = id.split(",");
			String prices[] = price.split(",");
			String quantitys[] = quantity.split(",");
			for (int i = 0; i < ids.length; i++) {
				ShopGoodsFragment.total = ShopGoodsFragment.total + (Double.valueOf(prices[i]) * Integer.valueOf(quantitys[i]));
				Log.d("xxx", "total: " + ShopGoodsFragment.total);
			}
		}
		BigDecimal bd = new BigDecimal(ShopGoodsFragment.total);
		bd=bd.setScale(2, BigDecimal.ROUND_UP);
		ShopGoodsFragment.totalTv.setText("总金额(￥): " + bd + "");
		ShopGoodsFragment.goodBtnOnClickListener.onClick(null);
	}

	/**
	 * 刷新购车列表
	 *
	 * @param context
	 */
	public void RefreshShoppinglist(Context context) {
		// 实例化SharedPreferences对象
		mySharedPreferences = context.getSharedPreferences("shoppingcart",
				Activity.MODE_PRIVATE);
		// 实例化SharedPreferences.Editor对象（第二步）
		editor = mySharedPreferences.edit();
		// 用putString的方法保存数据
		String id = mySharedPreferences.getString("id", "");
		if (id == "") {// 判断是否有数据
			editor.putString("id", good.getId() + "");
			editor.putString("name", good.getName());
			editor.putString("image", good.getImage());
			editor.putString("price", good.getPrice()+"");
			editor.putString("stock", good.getStock()+"");
			editor.putString("quantity", "1");
			editor.commit();
		} else {
			Log.i("hjw", "有数据，进来了");
			String name = mySharedPreferences.getString("name", "");
			String image = mySharedPreferences.getString("image", "");
			String price = mySharedPreferences.getString("price", "");
			String stock = mySharedPreferences.getString("stock", "");
			String quantity = mySharedPreferences.getString("quantity", "");
			String ids[] = id.split(",");
			String quantitys[] = quantity.split(",");
			String stocks[] = stock.split(",");
			if (index == 0) {
				for (int i = 0; i < ids.length; i++) {
					if (ids[i].equals(good.getId() + "")) {// 判断id是否已经存在
						Log.i("hjw", "存在了");
						quantitys[i] = (Integer.valueOf(quantitys[i]) + 1) + "";// 修改该商品的数量++
						for (int j = 0; j < quantitys.length; j++) {
							if (j == 0) {
								quantity = quantitys[j];
							} else {
								quantity = quantity + "," + quantitys[j];
							}
						}
						editor.putString("quantity", quantity);
						editor.commit();
						return;
					}
				}
				Log.i("hjw", "添加一个商品" + good.getId());
				editor.putString("id", id + "," + good.getId());
				editor.putString("name", name + "," + good.getName());
				editor.putString("image", image + "," + good.getImage());
				editor.putString("price", price + "," + good.getPrice());
				editor.putString("stock", stock + "," + good.getStock());
				editor.putString("quantity", quantity + ",1");
				editor.commit();
			} else {
				for (int i = 0; i < ids.length; i++) {
					if (ids[i].equals(good.getId() + "")) {// 判断id是否已经存在
						quantitys[i] = (Integer.valueOf(quantitys[i]) - 1) + "";// 修改该商品的数量--
						for (int j = 0; j < quantitys.length; j++) {
							if (j == 0) {
								quantity = quantitys[j];
							} else {
								quantity = quantity + "," + quantitys[j];
							}
						}

						editor.putString("quantity", quantity);
						editor.commit();
						return;
					}
				}
			}
		}

	}
}
